﻿using System;
using System.Collections.Generic;

namespace duocon.data
{
    internal class DuoconSegment<TKey, TValue>
    {
        internal readonly Dictionary<TKey, TValue> _dict;
        private readonly object _lock = new object();

        public DuoconSegment(int capacity)
        {
            _dict = new Dictionary<TKey, TValue>(capacity);
        }

        public bool TryGetValue(TKey key, out TValue value)
        {
            lock (_lock)
                return _dict.TryGetValue(key, out value);
        }

        public void AddOrUpdate(TKey key, TValue value)
        {
            lock (_lock)
                if (_dict.ContainsKey(key))
                    _dict[key] = value;
                else
                    _dict.Add(key, value);
        }

        public bool Remove(TKey key)
        {
            lock (_lock)
                return _dict.Remove(key);
        }

        public TValue this[TKey key]
        {
            get
            {
                lock (_lock)
                    return _dict[key];
            }
            set
            {
                lock (_lock)
                    _dict[key] = value;
            }
        }

        public int Count
        {
            get
            {
                lock (_lock)
                    return _dict.Count;
            }
        }
    }
}
